#!/usr/bin/env python
# coding=utf-8
import math
# class tileEncoding:
#     str_code = ""
#     def __init__(self):


def base10toN(num, base):
    """Change ``num'' to given base
    Upto base 36 is supported."""
    converted_string, mod = "", ""
    currentnum = num
    if not 1 < base < 37:
        raise ValueError("base must be between 2 and 36")
    if not num:
        return '0'
    while currentnum:
        mod = currentnum % base
        currentnum = currentnum // base
        converted_string = chr(48 + mod + 7 * (mod > 10)) + converted_string
    return converted_string.lower()


def globalgrid(level, row, col):
    str_code = ""
    if col <= math.pow(2, level - 1):
        str_code += "1"
        if level > 1:
            str_code = left(str_code, level, row, col)
        b = base10toN(long(str_code, 5), 32)
    else:
        str_code += "2"
        if level > 1:
            str_code = right(
                str_code,
                level,
                row,
                col -
                math.pow(2, level -
                         1))
        b = base10toN(long(str_code, 5), 32)
    return b


def left(str_code, level, row, col):
    ranknumber = math.pow(2, level - 2)  # current row number
    if row <= ranknumber and col <= ranknumber:
        str_code += "1"
        if level > 2:
            str_code = left(str_code, level - 1, row, col)
    elif row <= ranknumber and col > ranknumber:
        str_code += "2"
        if level > 2:
            str_code = right(str_code, level - 1, row, col - ranknumber)
    elif row > ranknumber and col <= ranknumber:
        str_code += "3"
        if level > 2:
            str_code = left(str_code, level - 1, row - ranknumber, col)
    elif (row > ranknumber and col > ranknumber):
        str_code += "4"
        if level > 2:
            str_code = right(
                str_code,
                level - 1,
                row - ranknumber,
                col - ranknumber)
    return str_code


def right(str_code, level, row, col):
    ranknumber = math.pow(2, level - 2)  # current row number
    if row <= ranknumber and col > ranknumber:
        str_code += "1"
        if level > 2:
            str_code = right(str_code, level - 1, row, col - ranknumber)
    elif row <= ranknumber and col <= ranknumber:
        str_code += "2"
        if level > 2:
            str_code = left(str_code, level - 1, row, col)
    elif row > ranknumber and col > ranknumber:
        str_code += "3"
        if level > 2:
            str_code = right(
                str_code,
                level - 1,
                row - ranknumber,
                col - ranknumber)
    elif (row > ranknumber and col <= ranknumber):
        str_code += "4"
        if level > 2:
            str_code = left(str_code, level - 1, row - ranknumber, col)
    return str_code


def main():
    """docstr_codeing for main"""
    level = 19
    row = 131072
    col = 131072 * 2
    bstr_code = globalgrid(level, row, col)
    print(bstr_code)


if __name__ == '__main__':
    main()
